package pers.zh.parking.mvp.model;

import android.app.Application;

import com.google.gson.Gson;

import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import pers.zh.parking.common.ApiService;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.mvp.contract.MapContract;
import pers.zjc.commonlibs.util.StringUtils;

@ActivityScope
public class MapModel extends BaseModel implements MapContract.Model {

    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public MapModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<BaseResponse<List<ParkingLot>>> getLotsAround(double lat, double lng,
                                                                    int defaultRadius) {
        Map<String, Object> map = genQueryMap(lat, lng, defaultRadius, "");
        return mRepositoryManager.obtainRetrofitService(ApiService.class).getLotsAround(map);
    }

    private Map<String, Object> genQueryMap(double lat, double lng, int radius, String keyWord) {
        Map<String, Object> map = new HashMap<>();
        map.put("latitude", lat);
        map.put("longitude", lng);
        if (radius == 0) {
            radius = 50000;
        }
        map.put("radius", radius);
        if (!StringUtils.isEmpty(keyWord)) {
            map.put("keyword", keyWord);
        }
        return map;
    }

    @Override
    public Observable<BaseResponse<List<ParkingLot>>> searchLots(double lat, double lng,
                                                                 int defaultRadius,
                                                                 String keyWord) {
        Map<String, Object> map = genQueryMap(lat, lng, 0, keyWord);
        return mRepositoryManager.obtainRetrofitService(ApiService.class).searchLots(map);
    }
}